package com.wlf.controller;

import com.wlf.constant.Constants;
import com.wlf.entity.User;
import com.wlf.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author 作者 危凌峰
* @version 创建时间：2021年4月28日 上午9:35:15
*/
@Controller
public class LoginController {

	private static Logger logger = LoggerFactory.getLogger(LoginController.class);

	@Autowired
	private IUserService userService;

	/**
	 * 跳转到登陆页面
	 * @return
	 */
	@RequestMapping("/toLogin")
	public String toLogin(HttpServletRequest request, HttpServletResponse response){
		return "login";
	}

	/**
	 * 登陆方法，登陆成功跳转首页
	 * @return
	 */
	@RequestMapping(value = "/loginDo",method = RequestMethod.POST)
	public String loginDo(HttpServletRequest request, HttpServletResponse response){
		User user =new User();
		if(user != null){
			User loginUser =userService.getUser(user);
			if(loginUser != null){
				request.getSession().setAttribute(Constants.USER_SESSION,user);
				return "index";
			}
		}
		return "redirect:/toLogin";
	}

	/**
	 * 退出登陆，跳转到登陆页面
	 * @return
	 */
	@RequestMapping("/loginOut")
	public String loginOut(HttpServletRequest request, HttpServletResponse response){
		//TODO
		return "login";
	}

}
